package com.zk.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zk.entity.Orders;
import com.zk.entity.ResultDto;
import com.zk.service.IOrderDetailService;
import com.zk.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljw
 * @since 2020-11-29
 */
@RestController
@RequestMapping("/zk/order")
public class OrdersController {
    @Autowired
    private IOrdersService orderService;
    /**
     * 添加订单
     */
    @Transactional
    @RequestMapping(path="/",method= RequestMethod.POST)
    public ResultDto<Orders> insertOrder(
            @RequestBody Orders orders
    ){
        try {
            if(orderService.addOrders(orders)){
                return new ResultDto<>(200, "订单添加成功");
            }else {
                return new ResultDto<>(200, "订单添加失败");
            }
        }catch (Exception e){
            System.out.println(e);
            return new ResultDto<>(500, "服务器内部错误");
        }
    }

    /**
     * 删除订单
     */
    @RequestMapping(path="/",method= RequestMethod.DELETE)
    public ResultDto<Orders> delOrder(
            @RequestParam long orderId
    ){
        try {
            if(orderService.delOrders(orderId)){
                return new ResultDto<>(200, "订单删除成功");
            }else {
                return new ResultDto<>(200, "订单删除失败");
            }
        }catch (Exception e){
            System.out.println(e);
            return new ResultDto<>(500, "服务器内部错误");
        }
    }

    /**
     * 修改订单
     */
    @RequestMapping(path="/",method= RequestMethod.PUT)
    public ResultDto<Orders> updateOrder(
            @RequestBody Orders orders
    ){
        try {
            if(orderService.updateOrders(orders)){
                return new ResultDto<>(200, "订单更新成功");
            }else {
                return new ResultDto<>(200, "订单更新失败");
            }
        }catch (Exception e){
            System.out.println(e);
            return new ResultDto<>(500, "服务器内部错误");
        }
    }
    /**
     * 查看所有订单
     */
    @RequestMapping(path="/",method = RequestMethod.GET)
    public ResultDto<IPage<Orders>> queryAllOrder(
            @RequestParam Integer current,
            @RequestParam Integer size
    ){
        return new ResultDto<>(200, "订单查找成功",orderService.queryAllOrders(current,size));
    }

    /**
     * 根据ID查看订单
     */
    @RequestMapping(path="/{orderId}",method = RequestMethod.GET)
    public ResultDto<Orders> queryOrderById(
            @PathVariable Long orderId
    ){
        return new ResultDto<>(200, "订单查找成功",orderService.queryOrdersById(orderId));
    }

    /**
     * 根据用户ID查看订单
     */
    @RequestMapping(path="/user/{userId}",method = RequestMethod.GET)
    public ResultDto<IPage<Orders>> queryOrderByUserId(
            @PathVariable Long userId,
            @RequestParam Integer current,
            @RequestParam Integer size
    ){
        return new ResultDto<>(200, "订单查找成功",orderService.queryOrdersByUserName(userId,current,size));
    }


}

